home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / tm / tm-parse.el.z / tm-parse.el
Encoding:
Text File  |  1998-05-21  |  6.6 KB  |  235 lines

  1. ;;; tm-parse.el --- MIME message parser
  2.  
  3. ;; Copyright (C) 1994,1995,1996 Free Software Foundation, Inc.
  4.  
  5. ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
  6. ;; Version: $Id: tm-parse.el,v 7.16 1996/12/17 11:44:55 morioka Exp $
  7. ;; Keywords: mail, news, MIME, multimedia
  8.  
  9. ;; This file is part of tm (Tools for MIME).
  10.  
  11. ;; This program is free software; you can redistribute it and/or
  12. ;; modify it under the terms of the GNU General Public License as
  13. ;; published by the Free Software Foundation; either version 2, or (at
  14. ;; your option) any later version.
  15.  
  16. ;; This program is distributed in the hope that it will be useful, but
  17. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  19. ;; General Public License for more details.
  20.  
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs; see the file COPYING.  If not, write to the
  23. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  24. ;; Boston, MA 02111-1307, USA.
  25.  
  26. ;;; Code:
  27.  
  28. (require 'std11)
  29. (require 'tl-misc)
  30. (require 'tm-def)
  31.  
  32.  
  33. ;;; @ field parser
  34. ;;;
  35.  
  36. (defconst rfc822/quoted-pair-regexp "\\\\.")
  37. (defconst rfc822/qtext-regexp
  38.   (concat "[^" (char-list-to-string std11-non-qtext-char-list) "]"))
  39. (defconst rfc822/quoted-string-regexp
  40.   (concat "\""
  41.       (regexp-*
  42.        (regexp-or rfc822/qtext-regexp rfc822/quoted-pair-regexp)
  43.        )
  44.       "\""))
  45.  
  46. (defconst mime/content-parameter-value-regexp
  47.   (concat "\\("
  48.       rfc822/quoted-string-regexp
  49.       "\\|[^; \t\n]*\\)"))
  50.  
  51. (defconst mime::parameter-regexp
  52.   (concat "^[ \t]*\;[ \t]*\\(" mime/token-regexp "\\)"
  53.       "[ \t]*=[ \t]*\\(" mime/content-parameter-value-regexp "\\)"))
  54.  
  55. (defun mime/parse-parameter (str)
  56.   (if (string-match mime::parameter-regexp str)
  57.       (let ((e (match-end 2)))
  58.     (cons
  59.      (cons (downcase (substring str (match-beginning 1) (match-end 1)))
  60.            (std11-strip-quoted-string
  61.         (substring str (match-beginning 2) e))
  62.            )
  63.      (substring str e)
  64.      ))))
  65.  
  66. (defconst mime::ctype-regexp (concat "^" mime/content-type-subtype-regexp))
  67.  
  68. (defun mime/parse-Content-Type (string)
  69.   "Parse STRING as field-body of Content-Type field. [tm-parse.el]"
  70.   (setq string (std11-unfold-string string))
  71.   (if (string-match mime::ctype-regexp string)
  72.       (let* ((e (match-end 0))
  73.          (ctype (downcase (substring string 0 e)))
  74.          ret dest)
  75.     (setq string (substring string e))
  76.     (while (setq ret (mime/parse-parameter string))
  77.       (setq dest (cons (car ret) dest)
  78.         string (cdr ret))
  79.       )
  80.     (cons ctype (nreverse dest))
  81.     )))
  82.  
  83. (defconst mime::dtype-regexp (concat "^" mime/disposition-type-regexp))
  84.  
  85. (defun mime/parse-Content-Disposition (string)
  86.   "Parse STRING as field-body of Content-Disposition field. [tm-parse.el]"
  87.   (setq string (std11-unfold-string string))
  88.   (if (string-match mime::dtype-regexp string)
  89.       (let* ((e (match-end 0))
  90.          (ctype (downcase (substring string 0 e)))
  91.          ret dest)
  92.     (setq string (substring string e))
  93.     (while (setq ret (mime/parse-parameter string))
  94.       (setq dest (cons (car ret) dest)
  95.         string (cdr ret))
  96.       )
  97.     (cons ctype (nreverse dest))
  98.     )))
  99.  
  100.  
  101. ;;; @ field reader
  102. ;;;
  103.  
  104. (defun mime/Content-Type ()
  105.   "Read field-body of Content-Type field from current-buffer,
  106. and return parsed it. [tm-parse.el]"
  107.   (let ((str (std11-field-body "Content-Type")))
  108.     (if str
  109.     (mime/parse-Content-Type str)
  110.       )))
  111.  
  112. (defun mime/Content-Transfer-Encoding (&optional default-encoding)
  113.   "Read field-body of Content-Transfer-Encoding field from
  114. current-buffer, and return it.
  115. If is is not found, return DEFAULT-ENCODING. [tm-parse.el]"
  116.   (let ((str (std11-field-body "Content-Transfer-Encoding")))
  117.     (if str
  118.     (progn
  119.       (if (string-match "[ \t\n\r]+$" str)
  120.           (setq str (substring str 0 (match-beginning 0)))
  121.         )
  122.       (downcase str)
  123.       )
  124.       default-encoding)
  125.     ))
  126.  
  127. (defun mime/Content-Disposition ()
  128.   "Read field-body of Content-Disposition field from current-buffer,
  129. and return parsed it. [tm-parse.el]"
  130.   (let ((str (std11-field-body "Content-Disposition")))
  131.     (if str
  132.     (mime/parse-Content-Disposition str)
  133.       )))
  134.  
  135.  
  136. ;;; @ message parser
  137. ;;;
  138.  
  139. (define-structure mime::content-info
  140.   rcnum point-min point-max type parameters encoding children)
  141.  
  142.  
  143. (defun mime/parse-multipart (boundary ctype params encoding rcnum)
  144.   (goto-char (point-min))
  145.   (let* ((dash-boundary   (concat "--" boundary))
  146.      (delimiter       (concat "\n" (regexp-quote dash-boundary)))
  147.      (close-delimiter (concat delimiter "--[ \t]*$"))
  148.      (beg (point-min))
  149.      (end (progn
  150.         (goto-char (point-max))
  151.         (if (re-search-backward close-delimiter nil t)
  152.             (match-beginning 0)
  153.           (point-max)
  154.           )))
  155.      (rsep (concat delimiter "[ \t]*\n"))
  156.      (dc-ctl
  157.       (if (string-equal ctype "multipart/digest")
  158.           '("message/rfc822")
  159.         '("text/plain")
  160.         ))
  161.      cb ce ct ret ncb children (i 0))
  162.     (save-restriction
  163.       (narrow-to-region beg end)
  164.       (goto-char beg)
  165.       (re-search-forward rsep nil t)
  166.       (setq cb (match-end 0))
  167.       (while (re-search-forward rsep nil t)
  168.     (setq ce (match-beginning 0))
  169.     (setq ncb (match-end 0))
  170.     (save-restriction
  171.       (narrow-to-region cb ce)
  172.       (setq ret (mime/parse-message dc-ctl "7bit" (cons i rcnum)))
  173.       )
  174.     (setq children (cons ret children))
  175.     (goto-char (mime::content-info/point-max ret))
  176.     (goto-char (setq cb ncb))
  177.     (setq i (1+ i))
  178.     )
  179.       (setq ce (point-max))
  180.       (save-restriction
  181.     (narrow-to-region cb ce)
  182.     (setq ret (mime/parse-message dc-ctl "7bit" (cons i rcnum)))
  183.     )
  184.       (setq children (cons ret children))
  185.       )
  186.     (mime::content-info/create rcnum beg (point-max)
  187.                    ctype params encoding
  188.                    (nreverse children))
  189.     ))
  190.  
  191. (defun mime/parse-message (&optional ctl encoding rcnum)
  192.   "Parse current-buffer as a MIME message. [tm-parse.el]"
  193.   (setq ctl (or (mime/Content-Type) ctl))
  194.   (setq encoding (or (mime/Content-Transfer-Encoding) encoding))
  195.   (let ((ctype (car ctl))
  196.     (params (cdr ctl))
  197.     )
  198.     (let ((boundary (assoc "boundary" params)))
  199.       (cond (boundary
  200.          (setq boundary (std11-strip-quoted-string (cdr boundary)))
  201.          (mime/parse-multipart boundary ctype params encoding rcnum)
  202.          )
  203.         ((or (string-equal ctype "message/rfc822")
  204.          (string-equal ctype "message/news")
  205.          )
  206.          (goto-char (point-min))
  207.          (mime::content-info/create rcnum
  208.                     (point-min) (point-max)
  209.                     ctype params encoding
  210.                     (save-restriction
  211.                       (narrow-to-region
  212.                        (if (re-search-forward "^$" nil t)
  213.                            (1+ (match-end 0))
  214.                          (point-min)
  215.                          )
  216.                        (point-max))
  217.                       (list (mime/parse-message
  218.                          nil nil (cons 0 rcnum)))
  219.                       )
  220.                     )
  221.          )
  222.         (t 
  223.          (mime::content-info/create rcnum (point-min) (point-max)
  224.                     ctype params encoding nil)
  225.          ))
  226.       )))
  227.  
  228.  
  229. ;;; @ end
  230. ;;;
  231.  
  232. (provide 'tm-parse)
  233.  
  234. ;;; tm-parse.el ends here
  235.